package com.open.source.ams.metadata;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import lombok.NoArgsConstructor;

/**
 * @author ZonLen since on 2022/3/10 下午3:03
 */
@NoArgsConstructor
public class ValidationPropertyAnnotationMetadata {

  private String validateAnnotationType;

  private String validateAnnotationValue;

  private boolean javaxAnnotation;

  public ValidationPropertyAnnotationMetadata(Annotation annotation) {
    this.validateAnnotationType = annotation.annotationType().getName();
    this.javaxAnnotation = validateAnnotationType.contains("javax.validation.constraints");
    if (javaxAnnotation) {
      try {
        this.validateAnnotationValue = annotation.getClass().getMethod("message").invoke(annotation)
            .toString();
      } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        throw new RuntimeException("获取javax注解message信息异常", e);
      }
    }
  }


  public String getValidateAnnotationType() {
    return validateAnnotationType;
  }

  public String getValidateAnnotationMessage() {
    return validateAnnotationValue;
  }

  public void setValidateAnnotationType(String validateAnnotationType) {
    this.validateAnnotationType = validateAnnotationType;
  }

  public void setValidateAnnotationMessage(String validateAnnotationMessage) {
    this.validateAnnotationValue = validateAnnotationMessage;
  }

  @Override
  public String toString() {
    return "ValidationAnnotationMetadata{" +
        "validateAnnotationType='" + validateAnnotationType + '\'' +
        ", validateAnnotationValue='" + validateAnnotationValue + '\'' +
        '}';
  }

  public boolean isJavaxAnnotation() {
    return javaxAnnotation;
  }
}
